﻿using Newtonsoft.Json;
using Omabar.BLL;
using Omabar.Common;
using Omabar.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;

namespace Omabar.CMS.Handler
{
    /// <summary>
    /// HotelAdd 的摘要说明
    /// </summary>
    public class HotelAdd : IHttpHandler, IRequiresSessionState
    {
        public void ProcessRequest(HttpContext context)
        {
            RESP_Info info = new RESP_Info();
            try
            {
                context.Response.ContentType = "text/plain";
                string HOTEL_NAME = context.Request["HOTEL_NAME"];
                string HOTEL_DES = context.Request["HOTEL_DES"];
                string HOTEL_WEBSITE = context.Request["HOTEL_WEBSITE"];
                string HOTEL_ADDRESS = context.Request["HOTEL_ADDRESS"];
                string HOTE_DETAIL = context.Request["HOTE_DETAIL"];

                HOTEL hotel = new HOTEL()
                {
                    HOTEL_ID = Util.GetUID(),
                    HOTEL_NAME = HOTEL_NAME,
                    HOTEL_DES = HOTEL_DES,
                    HOTEL_ADDRESS = HOTEL_ADDRESS,
                    HOTEL_WEBSITE = HOTEL_WEBSITE,
                    HOTE_DETAIL = HOTE_DETAIL
                };

                Hotel_BLL bll = new Hotel_BLL();


                string[] photos = string.IsNullOrEmpty(context.Request["photo[0][]"]) ? new string[] { } : context.Request["photo[0][]"].Split(',');
                Hotel_Gallery_BLL hotelGalleryBLL = new Hotel_Gallery_BLL();
                foreach (string photo in photos)
                {
                    info = hotelGalleryBLL.Add(new HOTEL_GALLERY()
                    {
                        HOTEL_ID = hotel.HOTEL_ID,
                        PHOTO_ID = photo
                    });

                    System.Threading.Thread.Sleep(100);
                }

                info = bll.Add(hotel);
            }
            catch (Exception ex)
            {
                Tom.Net.Log.LogManager.Error("添加酒店失败", ex);
                info.Success = false;
                info.Title = "错误";
                info.Tip = "<h5><p>添加酒店失败</p></h5>";
                info.Url = "HotelAdd.aspx";
            }

            context.Response.Write(JsonConvert.SerializeObject(info));
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}